<template>
  <div>
    <van-nav-bar left-arrow @click-left="onClickLeft" title="收货地址" />
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
    />
  </div>
</template>

<script>
import { addressList, getCurUserId ,checktoken} from "../request/api.js";
export default {
  data() {
    return {
      chosenAddressId: "1",
      list: [
        // {
        //   id: "1",
        //   name: "张三",
        //   tel: "13000000000",
        //   address: "浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室",
        //   isDefault: true,
        // },
      ],
    };
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
    onAdd() {
      this.$router.push("/shouhuo");
    },
    onEdit(item) {
      this.$router.push('addressedit?id='+item.id)
    },
  },
  mounted() {
    let uid = getCurUserId();
    addressList({ uid })
      .then((d) => {
        //   console.log(d);
        if (d.data.code == 200) {
             let listArr = []
          for (const obj of d.data.list) {

               let obj2 = { id: obj.id, name: obj.username, tel: obj.userphone, address:obj.useraddress };
               listArr.push(obj2)
          }
          this.list = listArr
        }
      })
      .catch((e) => {
        console.log(e);
      });
      //验证token是否有效
    checktoken()
      .then((d) => {
        if (d.data.code != 200) {
          this.$toast({
            message: d.data.msg,
            icon: "warning-o",
          });
        }
      })
      .catch((e) => {
        console.log(e);
      });
      
  },
};
</script>

<style scoped>
::v-deep .van-radio__icon--checked .van-icon {
  background-color: #ff6345 !important;
}
::v-deep .van-tag--danger {
  background-color: #ff6345;
}
::v-deep .van-nav-bar__content {
  background-color: #ff6345;
}
.van-button--danger {
  background-color: #ff6345;
}
::v-deep .van-nav-bar__title {
  font-weight: bold;
  color: white;
}
::v-deep .van-nav-bar .van-icon {
  color: white;
  font-weight: bold;
}
</style>